package logic.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ciko.code.logic.db.ContactsDBHelper2;

import java.util.ArrayList;
import java.util.List;

import logic.bean.GroupBean;
import logic.db.ContactsDBHelper;
import logic.table.GroupTable;


/**
 * 联系人分组 数据操作类
 * 
 * @author 刘宾
 * @date 2015年12月15日 下午3:12:16
 */
public class GroupDao
{
	private static GroupDao  dao;
	private ContactsDBHelper2 dbHelper;
	
	private GroupDao(Context context) {
		dbHelper = ContactsDBHelper2.getInstance(context);
	}
	
	public static GroupDao getInstance(Context context) {
		dao = new GroupDao(context);
		return dao;
	}
	
	private ContentValues getContentValues(GroupBean bean,boolean isInsert) {
		final ContentValues values = new ContentValues();
		if(isInsert){
			values.put(GroupTable.GroupColums.GROUP_ID, bean.getGroupId());
		}
		values.put(GroupTable.GroupColums.NAME, bean.getName());
		values.put(GroupTable.GroupColums.SORT_NUM, bean.getSortNum());
		values.put(GroupTable.GroupColums.IS_DELETE, bean.getIsDelete());
		return values;
	}
	
	public boolean add(GroupBean bean) {
		try
		{
			dbHelper.getSqliteDatabaseW().insert(GroupTable.table, null, getContentValues(bean,true));
			return true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeSqliteDatabaseW();
		}
		return false;
	}
	
	public boolean addList(List<GroupBean> list) {
		SQLiteDatabase db = null;
		try
		{
			db = dbHelper.getSqliteDatabaseW();
			for (int i = 0; i < list.size(); i++)
			{
				db.insert(GroupTable.table, null, getContentValues(list.get(i),true));
			}
			db.setTransactionSuccessful();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			try
			{
				db.endTransaction();
				db.close();
				dbHelper.closeSqliteDatabaseW();
			}
			catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}
	
	public boolean update(GroupBean bean) {
		final String selection = GroupTable.GroupColums.GROUP_ID + " = ? ";
		final String[] args = new String[] {
			String.valueOf(bean.getGroupId())
		};
		int v = 0;
		try
		{
			v = dbHelper.getSqliteDatabaseW().update(GroupTable.table, getContentValues(bean,false), selection, args);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeSqliteDatabaseW();
		}
		return v > 0 ? true : false;
	}
	
	public boolean delete(int groupId) {
		final String selection = GroupTable.GroupColums.GROUP_ID + " = ? ";
		final String[] args = new String[] {
			String.valueOf(groupId)
		};
		int v = dbHelper.getSqliteDatabaseW().delete(GroupTable.table, selection, args);
		dbHelper.closeSqliteDatabaseW();
		return v > 0 ? true : false;
	}
	public boolean deleteAll() {
		int v = dbHelper.getSqliteDatabaseW().delete(GroupTable.table, null, null);
		dbHelper.closeSqliteDatabaseW();
		return v > 0 ? true : false;
	}
	
	public GroupBean getById(int groupId) {
		GroupBean bean = null;
		final String selection = GroupTable.GroupColums.GROUP_ID + " = ? ";
		final String[] args = new String[] {
			String.valueOf(groupId)
		};
		Cursor c = dbHelper.getSqliteDatabaseR().query(GroupTable.table, null, selection, args, null, null, null);
		try
		{
			if (c != null && c.getCount() > 0)
			{
				c.moveToFirst();
				bean = new GroupBean(c.getInt(c.getColumnIndex(GroupTable.GroupColums.GROUP_ID)), 
				                     c.getString(c.getColumnIndex(GroupTable.GroupColums.IS_DELETE)), 
				                     c.getString(c.getColumnIndex(GroupTable.GroupColums.NAME)), 
				                     c.getInt(c.getColumnIndex(GroupTable.GroupColums.SORT_NUM)));
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
			dbHelper.closeSqliteDatabaseR();
		}
		
		return bean;
	}
	
	public List<GroupBean> getAll() {
		List<GroupBean> list = null;
		Cursor c = dbHelper.getSqliteDatabaseR().query(GroupTable.table, null, null, null, null, null, null);
		try
		{
			if (c != null && c.getCount() > 0)
			{
				list = new ArrayList<GroupBean>();
				while (c.moveToNext())
				{
					list.add(new GroupBean(c.getInt(c.getColumnIndex(GroupTable.GroupColums.GROUP_ID)), 
					                       c.getString(c.getColumnIndex(GroupTable.GroupColums.IS_DELETE)), 
					                       c.getString(c.getColumnIndex(GroupTable.GroupColums.NAME)), 
					                       c.getInt(c.getColumnIndex(GroupTable.GroupColums.SORT_NUM))));
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			dbHelper.closeCursor(c);
			dbHelper.closeSqliteDatabaseR();
		}
		
		return list;
	}
}
